from zope.interface import implements

from indra.base import llsd
import urllib2

from interfaces import ISecondLifeLoginHandler

class SLLoginHandler(object):
    """implements a login handler"""
    
    implements(ISecondLifeLoginHandler)
    
    def __init__(self,agent):
        """initialize the login handler with an (empty) agent object which implement ISecondLifeAgent"""
        self.agent = agent
        
    def login(self,url="https://login.aditi.lindenlab.com/cgi-bin/auth.cgi"):
        """login the agent to the agent domain"""
        
        login={
               'firstname'        : self.agent.firstname,
               'lastname'         : self.agent.lastname,
               'md5-password'     : self.agent.password,
        }

        llsdlist = llsd.format_xml(login)
        headers = {
                    "Content-type": "application/llsd+xml",
                    "Accept": "*/*",
                  }
        r = urllib2.Request(url,llsdlist,headers)
        try:
            res = urllib2.urlopen(r)
        except urllib2.HTTPError, e:
            print "*** Exception on Login: ", e
            print e.read()
            raise

        # should it work someday I could go on ;-)
         
        